<?php

class LoanWriteOffPayments extends HTech_Db_Table
{
    protected $_name     = 'view_loan_write_off_payments';
    protected $_primary  = 'WriteOffPaymentId';
    
    protected $_referenceMap    = array(
        'Loans' => array(
            'columns'           => array('LoanId'),
            'refTableClass'     => 'Loans',
            'refColumns'        => array('LoanId')
        ),
        'LoanWriteOffs' => array(
            'columns'           => array('LoanId'),
            'refTableClass'     => 'LoanWriteOffs',
            'refColumns'        => array('LoanId')
        )
    );
    
    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Legal payments");
        $this->_gridMetadata['params'] = array('LoanId');
        $this->_gridMetadata['headers'] = array
            (
                array('WriteOffPaymentId', 50, $this->translate("Id"),       'Number'),
                array('Amount',            80, $this->translate("Amount"),   'Number',   'Standard'),
                array('LastEditDate',     150, $this->translate("Date"),     'DateTime', 'Date'),
                array('User',             100, $this->translate("User"),     'String')
            );
        return $this;
    }
    
    protected function _addCallback($id, $data) 
    {
        $loan = new Emms_WrittenOffLoan($data['LoanId']);
        if($loan->get('TotalBalance') <= 0)
            $loan->setStatus('Recovered');
        return $id; 
    }
}